<template>
  <div>
    <div class="my-form-container"
         style="width: 100% !important; height: 100%;margin:0">
      <el-form :model="formData"
               :rules="rules"
               ref="ruleForm"
               hide-required-asterisk="true">
        <div style="display: flex;">
          <el-form-item class="my-form-item"
                        prop="className">
            <div class="my-form-item-label">
              专业班次
              <span class="mustW">*</span>
            </div>
            <el-input v-model="formData.className"
                      class="itemInput"
                      placeholder="请输入专业班次" />
          </el-form-item>
          <el-form-item class="my-form-item"
                        prop="alias">
            <div class="my-form-item-label">
              别名
              <span class="mustW">*</span>
            </div>
            <el-input v-model="formData.alias"
                      class="itemInput"
                      placeholder="请输入别名" />
          </el-form-item>
        </div>
        <div style="display: flex;">
          <el-form-item class="my-form-item"
                        prop="studenteamname">
            <div class="my-form-item-label">
              学员队
              <span class="mustW">*</span>
            </div>
            <el-select v-model="formData.studenteamname"
                       class="itemInput"
                       clearable
                       placeholder="请选择学员队"
                       style="width: 100%;"
                       @input="handleChangeTeam">
              <el-option v-for="(item, index) in xydList"
                         :key="index"
                         :label="item.deptName"
                         :value="index" />
            </el-select>
          </el-form-item>
          <el-form-item class="my-form-item"
                        prop="year">
            <div class="my-form-item-label">
              年级
              <span class="mustW">*</span>
            </div>
            <el-select v-model="formData.year"
                       class="itemInput"
                       clearable
                       placeholder="请选择年级"
                       @change="changeSpe(formData.year)">        
              <el-option 
                v-for="(item, index) in njList"
                :key="index"
                :label="item.xqszxn"
                :value="item.xqszxn" 
              />
            </el-select>
          </el-form-item>
        </div>
        <div style="display: flex;">
          <el-form-item class="my-form-item"
                        prop="speName">
            <div class="my-form-item-label">
              专业【层次】
              <span class="mustW">*</span>
            </div>
            <el-select v-model="formData.speName"
                       class="itemInput"
                       clearable
                       placeholder="请选择专业【层次】"
                       style="width: 100%;"
                       @input="handleChangePlan">
              <el-option v-for="(item, index) in zyList"
                         :key="item.id"
                         :label="item.trainingLevelname != '' ? item.specialtyName + '【' + item.trainingLevelname + '】' : item.specialtyName"
                         :value="index" />
            </el-select>
          </el-form-item>
          <el-form-item class="my-form-item"
                        prop="campus">
            <div class="my-form-item-label">
              所在校区
              <!-- <span class="mustW">*</span> -->
            </div>
            <el-select v-model="formData.campus"
                       class="itemInput"
                       clearable
                       placeholder="请选择所在校区">
              <el-option v-for="(item, index) in xqList"
                         :key="index"
                         :label="item.deptName"
                         :value="item.id" />
            </el-select>
          </el-form-item>
        </div>
        <div style="display: flex;">
          <el-form-item class="my-form-item"
                        prop="ratedNumber">
            <div class="my-form-item-label">
              额定人数
              <span class="mustW">*</span>
            </div>
            <el-input v-model="formData.ratedNumber"
                      type="number"
                      controls-position="right"
                      class="itemInput"
                      placeholder="请输入额定人数" />
          </el-form-item>
          <el-form-item class="my-form-item"
                        prop="homepage">
            <div class="my-form-item-label">
              专业班次主页
            </div>
            <el-input v-model="formData.homepage"
                      class="itemInput"
                      placeholder="请输入专业班次主页" />
          </el-form-item>
        </div>
        <div style="display: flex;">
          <el-form-item class="my-form-item"
                        prop="studyRoom">
            <div class="my-form-item-label">
              固定上课教室
            </div>
            <el-input readonly
                      style="width:55%"
                      v-model="formData.studyRoom"
                      class="itemInput"
                      placeholder="请选择固定上课教室">
            </el-input>
            <el-button icon="el-icon-edit"
                       style="position: relative;top: 24px;right: 23px;height:40px;background: #f1fff8; border: 1px solid #02c86c; border-radius: 6px; color: #02c86c;"
                       @click="handleSelectRoom('course')" />
          </el-form-item>
          <el-form-item class="my-form-item"
                        prop="selfStudyRoom">
            <div class="my-form-item-label">
              固定自习教室
            </div>
            <el-input readonly
                      style="width:55%"
                      v-model="formData.selfStudyRoom"
                      class="itemInput"
                      placeholder="请选择固定自习教室">
            </el-input>
            <el-button icon="el-icon-edit"
                       style="position: relative;top: 24px;right: 23px;height:40px;background: #f1fff8; border: 1px solid #02c86c; border-radius: 6px; color: #02c86c;"
                       @click="handleSelectRoom('study')" />
          </el-form-item>
        </div>
        <div style="display: flex;">
          <el-form-item class="my-form-item"
                        prop="remark">
            <div class="my-form-item-label">
              备注
            </div>
            <el-input v-model="formData.remark"
                      type="textarea"
                      :rows="2"
                      placeholder="请输入备注"
                      style="width: 100%; margin: 13px 40px;" />
          </el-form-item>
        </div>
      </el-form>
    </div>
    <div style="display: flex;">
      <div class="report-button"
           style="width: 80px; height: 35px; line-height: 35px; background: #FFF6F0; border: 1px solid #E9B535; color: #E9B535;"
           @click="handleSave">
        <span>保存</span>
      </div>
      <div class="report-button"
           style="margin-left: -60px; width: 80px; height: 35px; line-height: 35px; background: #FFEEEF; border: 1px solid #FA533C; color: #FA533C;"
           @click="handleCancel">
        <span>取消</span>
      </div>
    </div>
    <el-dialog title="选择教室场地"
               :visible.sync="dialogShow"
               width="1500px">
      <div style="display: flex; border-bottom: 3px solid #427EBC; height: 50px; margin: 0 -25px;">
        <div :class="showType === 1 ? 'selectedStyle' : 'tabButton'"
             style="width: 90px;"
             @click="handleChangeType(1)">
          按建筑物
        </div>
        <div :class="showType === 2 ? 'selectedStyle' : 'tabButton'"
             style="width: 70px;"
             @click="handleChangeType(2)">
          按部门
        </div>
        <div :class="showType === 3 ? 'selectedStyle' : 'tabButton'"
             style="width: 70px;"
             @click="handleChangeType(3)">
          按类型
        </div>
        <div style="display: flex; margin-top: 9px; margin-left: 200px;">
          <el-input v-model="searchKey"
                    clearable
                    size="small"
                    placeholder="请输入场地场所名称" />
        </div>
        <div class="cjrc-button"
             style="margin: 11px 0 0 20px; width: 70px; background: #f1f8fe; border: 1px solid #51aef6; color: #51aef6;"
             @click="handleSearch">
          <span>查询</span>
        </div>
      </div>
      <div>
        <div style="float: left; width: 300px; height: 655px; border-right: 3px solid #427EBC; padding-top: 10px;">
          <el-tree v-show="showType !== 2"
                   :data="treeData1"
                   :props="treeProps1"
                   @node-click="handleNodeClick" />
          <el-tree v-show="showType === 2"
                   :data="treeData2"
                   :props="treeProps2"
                   @node-click="handleNodeClick" />
        </div>
        <div style="float: right; width: 1140px; height: 655px; padding-top: 20px;">
          <el-table :data="tableData"
                    @select="selectRow"
                    ref="multipleTable"
                    :height="530"
                    class="xpaas-table-class"
                    row-class-name="xpaas-row-class"
                    cell-class-name="xpaas-cell-class"
                    header-row-class-name="xpaas-header-row-class"
                    header-cell-class-name="xpaas-header-cell-class">
            <el-table-column width="65"
                             align="center"
                             type="selection" />
            <el-table-column prop="cdmc"
                             label="场地场所名称"
                             align="center" />
            <el-table-column prop="mj"
                             label="面积（平方米）"
                             align="center" />
            <el-table-column prop="krnrs"
                             label="容纳人数"
                             align="center" />
            <el-table-column prop="cdlx"
                             label="场地场所类型"
                             align="center" />
            <el-table-column prop="ssxq"
                             label="所属校区"
                             align="center" />
            <el-table-column prop="ssjzw"
                             label="所属建筑物"
                             align="center" />
            <el-table-column prop="mangdept"
                             label="管理部门"
                             align="center" />
            <el-table-column prop="col8"
                             label="楼层"
                             align="center" />
          </el-table>
          <div style="float: right; margin: 10px 0;">
            <el-pagination @size-change="handleSizeChange"
                           @current-change="handleCurrentChange"
                           :page-size="10"
                           layout="total, sizes, prev, pager, next, jumper"
                           :total="total">
            </el-pagination>
          </div>
          <div style="display: flex;">
            <div class="report-button"
                 style="margin-left: 450px; width: 80px; height: 35px; line-height: 35px; background: #FFF6F0; border: 1px solid #E9B535; color: #E9B535;"
                 @click="handleSure">
              <span>确定</span>
            </div>
            <div class="report-button"
                 style="width: 80px; height: 35px; line-height: 35px; background: #FFEEEF; border: 1px solid #FA533C; color: #FA533C;"
                 @click="dialogShow = false">
              <span>取消</span>
            </div>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getClassRoomTree,
  getDeptTree,
  querysppecltyId,
  submitSpecialty,
  tjqueryplaceinfo,
  placelike,
} from '@/api/classHoursAddList/addList'
import { reqXydList, reqCheckYearList,reqCheckLevelList } from '@/api/admin1/check'
import { loadDeptListByCategory } from '@/api/dept.js'
import { reqSpeByYear } from '@/api/classHoursAddList/addList'

export default {
  data() {
    return {
      optionType: this.$route.query.optionType,
      dialogShow: false,
      formData: {
        id: '',
        className: '',
        alias: '',
        studentTeam: '',
        studenteamname: '',
        year: '',
        speName: '',
        specialtyName: '',
        yearSpecialtyId: '',
        campus: '',
        ratedNumber: '',
        homepage: '',
        studyRoom: '',
        selfStudyRoom: '',
        remark: '',
      },
      treeProps1: {
        label: 'dictValue',
        children: 'childdictBizs',
      },
      treeProps2: {
        label: 'deptName',
        children: 'chilrenDept',
      },
      xydList: [],
      njList: [],
      zyList: [],
      xqList: [],
      rules: {
        className: [
          { required: true, message: '请输入专业班次', trigger: 'blur' },
        ],
        alias: [{ required: true, message: '请输入别名', trigger: 'blur' }],
        studenteamname: [
          { required: true, message: '请选择学员队', trigger: 'blur' },
        ],
        year: [{ required: true, message: '请选择年级', trigger: 'blur' }],
        speName: [
          { required: true, message: '请选择专业【层次】', trigger: 'blur' },
        ],
        // campus: [
        //   { required: true, message: '请选择所在校区', trigger: 'blur' },
        // ],
        ratedNumber: [
          { required: true, message: '请输入额定人数', trigger: 'blur' },
        ],
      },
      showType: 1,
      searchKey: '',
      treeData1: [],
      treeData2: [],
      tableData: [],
      handleSelection: [],
      handleSelectionStudy: [],
      roomType: '',
      total: '',
      pageNo: 1,
      pageSize: 10,
      ssjzwid: '',
      mangdeptid: '',
      cdlxid: '',
      eventData: {},
      trainingList:[],
    }
  },
  mounted: function () {
    reqXydList().then((res) => {
      this.xydList = res.data.data
    })
    reqCheckYearList().then((res) => {
      this.njList = res.data.data
    })
    // reqChoosezylist().then((res) => {
    //   this.zyList = res.data.data
    // })
    this.getXqList()
    let zybcid = this.$route.query.zybcid
    let year = this.$route.query.year
    if (zybcid === undefined) {
      zybcid = localStorage.getItem('classId')
    }
    localStorage.setItem('classId', zybcid)
    if (zybcid === '') {
      this.handleResetForm()
    } else {
      this.handleInitForm(zybcid)
    }
    if(year != '') {
      this.changeSpe(year)
    }
  },
  methods: {
    handleSelectionChangeS(val) {
      this.handleSelectionStudy.push(val)
    },
    handleSelectionChange(val) {
      this.handleSelection.push(val)
    },
    selectRow(val, row) {
      this.$refs.multipleTable.clearSelection()
      this.handleSelection = []
      this.handleSelectionStudy = []
      this.$refs.multipleTable.toggleRowSelection(row, true)
      // this.handleSelectionChange(row);
      if (this.roomType == 'course') {
        this.handleSelectionChange(row)
      } else {
        this.handleSelectionChangeS(row)
      }
    },
    //获取校区
    getXqList() {
      let deptCategory = '3'
      loadDeptListByCategory(deptCategory).then((res) => {
        this.xqList = res.data
      })
    },
    handleInitForm(id) {
      querysppecltyId(id).then((res) => {
        let data = res.data.data
        reqCheckLevelList().then(res=>{
          this.trainingList = res.data.data[0].children;
          let traInfo = this.trainingList.filter(item=>{
            return item.id == data.trainingLevel
          })
          this.formData = {
            id: data.id,
            className: data.className,
            alias: data.alias,
            studentTeam: data.studentTeam,
            studenteamname: data.studenteamname,
            year: data.year,
            specialtyName: data.specialtyName,
            speName: data.specialtyName + '【' + traInfo[0].dictValue + '】',
            campus: data.campus == '本部校区' ? '0' : '',
            ratedNumber: data.ratedNumber,
            homepage: data.homepage,
            studyRoom: data.studyRoom,
            selfStudyRoom: data.selfStudyRoom,
            remark: data.remark,
          }
        })
      })
    },
    changeSpe(year){
      this.formData.speName = '';
      reqSpeByYear(year).then((res) => {
        if (res.data.code === 200) {
          this.zyList = res.data.data
        }
      })
    },
    handleResetForm: function () {
      this.formData.className = ''
      this.formData.alias = ''
      this.formData.studentTeam = ''
      this.formData.year = ''
      this.formData.specialtyName = ''
      this.formData.campus = ''
      this.formData.ratedNumber = ''
      this.formData.zybczy = ''
      this.formData.studyRoom = ''
      this.formData.selfStudyRoom = ''
      this.formData.remark = ''
    },
    handleChangePlan(index) {
      this.formData.yearSpecialtyId = this.zyList[index].id
      this.formData.speName =
        this.zyList[index].trainingLevelname != ''
          ? this.zyList[index].specialtyName +
            '【' +
            this.zyList[index].trainingLevelname +
            '】'
          : this.zyList[index].specialtyName
      this.formData.specialtyName = this.zyList[index].specialtyName
    },
    handleChangeTeam(index) {
      this.formData.studentTeam = this.xydList[index].id
      this.formData.studenteamname = this.xydList[index].deptName
    },
    handleSelectRoom: function (val) {
      this.handleChangeType(1)
      this.dialogShow = true
      this.tableData = []
      this.roomType = val
      this.$refs.multipleTable.clearSelection()
    },
    //新增或编辑点击保存
    handleSave: function () {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          submitSpecialty(this.formData).then((res) => {
            console.log(res)
            if (res.data.code == 200) {
              this.$message({ type: 'success', message: '保存成功！' })
              this.handleResetForm()
              this.$router.push({
                path: '/admin/csTest/csTeat', //要跳转的页面的路由
              })
              return 'ok'
            }
          })
        }
      })
    },
    //场所弹框关闭
    handleSure: function () {
      this.dialogShow = false
      this.handleSelection.length > 0
        ? (this.formData.studyRoom = this.handleSelection[0].cdmc)
        : ''
      this.handleSelectionStudy.length > 0
        ? (this.formData.selfStudyRoom = this.handleSelectionStudy[0].cdmc)
        : ''
    },
    //取消新增或编辑
    handleCancel: function () {
      this.$router.push({ path: '/admin/csTest/csTeat' })
      this.handleResetForm()
    },
    handleChangeType: function (showType) {
      this.showType = showType
      this.treeData1 = []
      this.treeData2 = []
      if (showType === 1) {
        getClassRoomTree('cdcsType').then((res) => {
          if (res.data.code === 200) {
            this.treeData1 = res.data.data[0].childdictBizs
          }
        })
      } else if (showType === 2) {
        getDeptTree().then((res) => {
          if (res.data.code === 200) {
            this.treeData2 = res.data.data
          }
        })
      } else {
        getClassRoomTree('placetype').then((res) => {
          if (res.data.code === 200) {
            this.treeData1 = res.data.data
          }
        })
      }
    },
    handleNodeClick: function (event) {
      this.eventData = event
      let obj = {}
      obj.pageNo = this.pageNo
      obj.pageSize = this.pageSize
      if (this.showType === 1) {
        obj.ssjzwid = event.id
        this.ssjzwid = event.id
      } else if (this.showType === 2) {
        obj.mangdeptid = event.id
        this.mangdeptid = event.id
      } else if (this.showType === 3) {
        obj.cdlxid = event.id
        this.cdlxid = event.id
      }
      this.tjqueryplaceinfo(obj)
    },
    tjqueryplaceinfo(obj) {
      tjqueryplaceinfo(obj).then((res) => {
        if (res.data.code == 200) {
          this.tableData = res.data.data
          this.total = res.data.data.length > 0 ? res.data.data[0].tol : ''
        }
      })
    },
    handleSizeChange: function (val) {
      this.pageSize = val
      /* let obj = {
        pageNo:this.pageNo,
        pageSize: val,
        ssjzwid:this.ssjzwid,
        mangdeptid:this.mangdeptid,
        cdlxid:this.cdlxid
      }
      this.tjqueryplaceinfo(obj) */
      this.handleNodeClick(this.eventData)
    },
    handleCurrentChange: function (val) {
      this.pageNo = val
      /* let obj = {
        pageNo:val,
        pageSize: this.pageSize,
        ssjzwid:this.ssjzwid,
        mangdeptid:this.mangdeptid,
        cdlxid:this.cdlxid
      }
      this.tjqueryplaceinfo(obj) */
      this.handleNodeClick(this.eventData)
    },
    //弹框内场所查询
    handleSearch: function () {
      if (this.searchKey == '') {
        this.handleNodeClick(this.eventData)
      } else {
        let obj = {
          pageNo: this.pageNo,
          pageSize: this.pageSize,
          cdmc: this.searchKey,
        }
        placelike(obj).then((res) => {
          if (res.data.code == 200) {
            this.tableData = res.data.data
            this.total = res.data.data.length > 0 ? res.data.data[0].tol : ''
          }
        })
      }
    },
  },
}
</script>

<style lang="less" scoped>
/deep/.el-tree {
  color: #004ca7;
  .el-tree-node__content {
    .el-tree-node__label {
      // font-weight: 700;
    }
  }
}
.my-form-item {
  width: 100%;
  height: 85px;
  border-bottom: 1px solid rgba(0, 76, 167, 0.39);
  margin-bottom: 0;
  > div {
    display: flex;
    height: 100%;
    &:nth-child(2) {
      .my-form-item-label {
        border-left: 1px solid rgba(0, 76, 167, 0.39);
      }
    }
    .my-form-item-label {
      width: 220px;
      padding-left: 60px;
      background: rgba(0, 76, 167, 0.1);
      display: flex;
      flex: none;
      align-items: center;
      color: #004ca7;
      font-size: 16px;
      font-weight: bold;
      border-left: 1px solid rgba(0, 76, 167, 0.39);
      margin-left: -1px;
      .mustW {
        color: red;
      }
    }
    .el-form-item__content {
      display: flex;
    }
  }
}
.itemInput {
  width: 100%;
  margin: 22px 40px 0;
}
.report-button {
  height: 28px;
  line-height: 28px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  margin: 50px auto;
  img {
    width: 17px;
    height: 17px;
    background-size: 100% 100%;
    background-repeat: no-repeat;
    margin-right: 5px;
  }
}
.tabButton {
  text-align: center;
  height: 40px;
  line-height: 40px;
  margin-left: 25px;
  margin-top: 8px;
  border-top-left-radius: 6px;
  border-top-right-radius: 6px;
  background-color: #e8edf4;
  color: #0c4f9c;
  font-weight: 600;
  cursor: pointer;
}
.selectedStyle {
  text-align: center;
  height: 40px;
  line-height: 40px;
  margin-left: 25px;
  margin-top: 8px;
  border-top-left-radius: 6px;
  border-top-right-radius: 6px;
  background-color: #5080c0;
  color: #fdfefb;
  font-weight: 600;
  cursor: pointer;
}
.cjrc-button {
  height: 34px;
  line-height: 34px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  margin-right: 25px;
}
@fontFamily: 'Microsoft YaHei';
.xpaas-table-class {
  border: 1px solid #004ca7;
  border-radius: 9px;
  ::v-deep div.el-table__header-wrapper {
    table.el-table__header {
      thead {
        tr.xpaas-header-row-class {
          padding: 0;
          height: 76px;
          background-color: #edf2f9;
          font-size: 16px;
          font-family: Microsoft YaHei;
          font-weight: bold;
          line-height: 20px;
          color: #004ca7;
          th.xpaas-header-cell-class {
            font-weight: bold;
            border-right: 1px solid #c7d7eb;
            border-left: 0;
            border-bottom: 1px solid #c7d7eb;
            background-color: #edf2f9;
            div.cell {
              font-size: 16px;
              font-weight: bold;
              font-family: @fontFamily;
              color: #004ca7;
            }
          }
        }
      }
    }
  }
  ::v-deep div.el-table__body-wrapper {
    table.el-table__body {
      tbody {
        tr.xpaas-row-class {
          td.xpaas-header-cell-class {
            border-right: 1px solid #c7d7eb;
            border-left: 0;
            border-bottom: 1px solid #c7d7eb;
            background-color: #edf2f9;
            div.cell {
              font-size: 16px;
              font-weight: bold;
              font-family: @fontFamily;
              color: #004ca7;
            }
          }
          td.xpaas-cell-class {
            height: 76px;
            border-right: 1px solid #c7d7eb;
            border-left: 0;
            border-bottom: 1px solid #c7d7eb;
            div.cell {
              font-weight: normal;
              font-family: @fontFamily;
              color: #004ca7;
            }
          }
        }
      }
    }
  }
}
/deep/ .el-input-group__append {
  padding-left: 25px;
  border: none;
}
/deep/ .el-form-item__error {
  font-size: 12px;
  padding-top: 5px;
  position: absolute;
  top: 76%;
  left: 260px;
}
/deep/ .el-input-number .el-input__inner {
  text-align: left;
}
/deep/ .el-dialog {
  height: 800px;
  overflow: hidden;
}
/deep/ .el-tree {
  background-color: transparent;
}
</style>
